package com.ruoyi.web.controller.books;

import java.io.Console;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.framework.web.domain.server.Sys;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.books.domain.BooksMarks;
import com.ruoyi.books.service.IBooksMarksService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 非遗项目收藏Controller
 *
 * @author ruoyi
 * @date 2024-04-07
 */
@Anonymous
@ControllerAdvice
@RestController
@RequestMapping("/books/marks")
public class BooksMarksController extends BaseController
{
    @Autowired
    private IBooksMarksService booksMarksService;

    /**
     * 查询非遗项目收藏列表
     */
//    @PreAuthorize("@ss.hasPermi('books:marks:list')")
    @GetMapping("/list")
    public TableDataInfo list(BooksMarks booksMarks)
    {
        startPage();
        List<BooksMarks> list = booksMarksService.selectBooksMarksList(booksMarks);
        return getDataTable(list);
    }

    /**
     * 导出非遗项目收藏列表
     */
//    @PreAuthorize("@ss.hasPermi('books:marks:export')")
    @Log(title = "非遗项目收藏", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BooksMarks booksMarks)
    {
        List<BooksMarks> list = booksMarksService.selectBooksMarksList(booksMarks);
        ExcelUtil<BooksMarks> util = new ExcelUtil<BooksMarks>(BooksMarks.class);
        util.exportExcel(response, list, "非遗项目收藏数据");
    }

    /**
     * 获取非遗项目收藏详细信息
     */
//    @PreAuthorize("@ss.hasPermi('books:marks:query')")
    @GetMapping(value = "/{marksId}")
    public AjaxResult getInfo(@PathVariable("marksId") Long marksId)
    {
        return success(booksMarksService.selectBooksMarksByMarksId(marksId));
    }

    /**
     * 新增非遗项目收藏
     */
//    @PreAuthorize("@ss.hasPermi('books:marks:add')")
//    @Log(title = "非遗项目收藏", businessType = BusinessType.INSERT)
    @PostMapping
//    @RequestBody
    public AjaxResult add( BooksMarks booksMarks)
    {
        Long marksAllId = booksMarks.getMarksAllId();
        Long marksUserId = booksMarks.getMarksUserId();
        boolean unique = booksMarksService.checkAllUnique(marksAllId,marksUserId);
        if(!unique){
            return toAjax(booksMarksService.insertBooksMarks(booksMarks));
        }else {
            return error("已加入");
        }

    }

    /**
     * 修改非遗项目收藏
     */
//    @PreAuthorize("@ss.hasPermi('books:marks:edit')")
    @Log(title = "非遗项目收藏", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BooksMarks booksMarks)
    {
        return toAjax(booksMarksService.updateBooksMarks(booksMarks));
    }

    /**
     * 删除非遗项目收藏
     */
//    @PreAuthorize("@ss.hasPermi('books:marks:remove')")
    @Log(title = "非遗项目收藏", businessType = BusinessType.DELETE)
	@DeleteMapping("/{marksIds}")
    public AjaxResult remove(@PathVariable Long[] marksIds)
    {
        return toAjax(booksMarksService.deleteBooksMarksByMarksIds(marksIds));
    }
}
